package dao.admin.impl;

import dao.BaseDao;
import dao.admin.RulesDao;
import entity.Rules;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * RulesDaoImpl
 *
 * @author 谭文斌
 * @since 2023/9/5
 */
public class RulesDaoImpl extends BaseDao implements RulesDao {
    @Override
    public List<Rules> getList() {
        List<Rules> list = new ArrayList<>();
        String sql="select * from rules";
        Connection conn = getConnection();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()) {
                Rules rules=new Rules();
                rules.setId(rs.getInt("id"));
                rules.setBorrow_num(rs.getInt("borrow_num"));
                rules.setLimit_day(rs.getInt("limit_day"));
                rules.setBorrow_library(rs.getString("borrow_library"));
                rules.setOvertime_fee(rs.getFloat("overtime_fee"));
                list.add(rules);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,ps,conn);
        }
        return list;
    }

    @Override
    public int add(Rules rules) {
        String sql="insert into rules(borrow_num, limit_day, borrow_library, overtime_fee) values(?,?,?,?)";
        return super.executeUpdate(sql,rules.getBorrow_num(),rules.getLimit_day(),rules.getBorrow_library(),rules.getOvertime_fee());
    }

    @Override
    public int upd(Rules rules) {
        String sql = "update rules set borrow_num=?, limit_day=?, borrow_library=?,overtime_fee=? where id=?";
        return super.executeUpdate(sql, rules.getBorrow_num(), rules.getLimit_day(),rules.getBorrow_library(),rules.getOvertime_fee(),rules.getId());
    }

    @Override
    public int del(int id) {
        String sql = "delete from rules where id=?";
        return super.executeUpdate(sql,id);
    }
}
